

const { addMESSAGE, queryMESSAGE, deltetMESSAGE, getMESSAGEDetail } = require("../../../controller/context/message/MESSAGE.CONTROLLER");
const { serverError } = require("../../../utils");
const { emptyCheck } = require("../../../utils/checkEmpty");
/**
 * @description 留言路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/message");

// 添加或编辑
router.post("/add", async (ctx, next) => {
    try {

        const {        // shopId,
            _id = null, userName, articleId, msgContext
        } = ctx.request.body
        const check = emptyCheck({ userName, articleId, msgContext })
        if (check.code !== 0) {
            ctx.body = check
        } else {
            const res = await addMESSAGE({
                _id: _id,
                userName,
                articleId,
                msgContext,

            })
            ctx.body = res
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})

// // 查询列表
router.post("/list", async (ctx, next) => {
    try {
        const { current, pageSize, userName, articleId, _id } = ctx.request.body;
        const res = await queryMESSAGE({
            current, pageSize, userName, articleId, _id
        })
        ctx.body = res
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }

})


// 删除
router.post("/delete", async (ctx, next) => {
    try {
        const { ids = [] } = ctx.request.body
        if (ids.length === 0) {
            ctx.body = {
                code: 12,
                msg: "未选择需要删除的数据"
            }
        }
        else {
            const res = await deltetMESSAGE({ ids: ids })
            ctx.body = res;
        }
    } catch (error) {
        ctx.body = serverError
    }


})

// // 查看详情
router.post("/detail", async (ctx, next) => {
    try {
        const { _id } = ctx.request.body
        const check = emptyCheck({ _id })
        if (check.code !== 0) {
            ctx.body = check;
        } else {
            const res = await getMESSAGEDetail({ _id: _id })
            ctx.body = res;
        }

    } catch (error) {
        ctx.body = serverError
    }
})

module.exports = router